package com.design.pattern.observe.observe;

import com.design.pattern.observe.topic.WeatherData2;

import java.util.Observable;
import java.util.Observer;

/**
 * @author wangyingjie
 * @date
 * @remark 用jdk自带观察者模式，实现pull的通知方式
 */
public class CurrentConditionsDisplay2 implements Observer,DisplayElement {
    private float temp;
    private float humidity;

    /**
     * 观察者更新参数，自己从主题中拉参数
     * @param o
     * @param arg
     */
    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof WeatherData2){
            WeatherData2 obj = (WeatherData2) o;
            this.temp = obj.getTemp();
            this.humidity = obj.getHumidity();
            display();
        }
    }

    /**
     * 显示
     */
    @Override
    public void display() {
        System.out.println("CurrentCondion：温度是【"+temp+"】，湿度是【"+humidity+"】。");
    }

    public static void main(String[] args) {
        WeatherData2 weatherData2 = new WeatherData2();
        CurrentConditionsDisplay2 currentConditionsDisplay2 = new CurrentConditionsDisplay2();
        weatherData2.addObserver(currentConditionsDisplay2);
        weatherData2.setMessurement(1.11f,2.22f,3.33f);
    }
}
